<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * ProductType_Model class
 */
class ProductType_Model extends CI_Model {
	private static $instance;
	function __construct()
	{
		parent::__construct();
		self::$instance =& $this;
	}
	
	public static function getInstance()
	{
		return self::$instance;
	}
	
	public function getProductTypeById($id)
	{
		$productType = new ProductTypeObject();
		
		$this->db->where('id', $id);
		$data = $this->db->get(PRODUCT_TYPE_TABLE);
		
		if ($data->num_rows() > 0)
		{
			$row = $data->row();
			$productType->setId($row->id);
			$productType->setCategoryId($row->category_id);
			$productType->setTitle($row->title);
			$productType->setOrder($row->_order);
		}
		
		return $productType;
	}
	
	public function getProductTypesByCategoryId($catId)
	{
		$productTypes = array();
		$this->db->order_by('_order');
		$this->db->where('category_id', $catId);
		$data = $this->db->get(PRODUCT_TYPE_TABLE);
		
		foreach ($data->result() as $type)
		{
			$productType = new ProductTypeObject($type->id, $type->category_id, $type->title);
			$productTypes[] = $productType;
		}
		
		return $productTypes;
	}

	public function countProductTypeByCategoryId($categoryId)
	{
		$count = 0;
		$query = "SELECT COUNT(id) as total FROM ".PRODUCT_TYPE_TABLE." WHERE category_id=?";
		
		$data = $this->db->query($query, array($categoryId));
		
		if ($data->num_rows() > 0)
		{
			$row = $data->row();
			$count = $row->total;
		}
		
		return $count;
	}
	
	public function insertProductType($productType)
	{
		$query = "INSERT INTO ".PRODUCT_TYPE_TABLE. " VALUES(NULL, ?, ?, ?)";
		$order = $this->countProductTypeByCategoryId($productType->getCategoryId());
		
		$this->db->trans_start();
		$this->db->query($query, array($productType->getCategoryId(), $productType->getTitle(), $order + 1));
		$this->db->trans_complete();
		
		if ($this->db->trans_status() === FALSE)
			return INSERT_ERROR;
		
		// Update global variable: need to check performance
		Category_Model::getInstance()->getAllCategories();
		
		return ERROR_NONE;
	}
	
	public function deleteProductTypeById($id)
	{
		$query = "DELETE FROM ".PRODUCT_TYPE_TABLE." WHERE id=?";
		
		$this->db->trans_start();
		$this->db->query($query, array($id));
		$this->db->trans_complete();
		
		if ($this->db->trans_status() === FALSE)
			return DELETE_ERROR;
		
		// Update global variable: need to check performance
		Category_Model::getInstance()->getAllCategories();
		
		return ERROR_NONE;
	}
	
	public function updateProductType($id, $productType)
	{
		$query = "UPDATE ".PRODUCT_TYPE_TABLE." SET category_id=?, title=?, _order=? WHERE id=?";
		
		$this->db->trans_start();
		$this->db->query($query, array($productType->getCategoryId(), $productType->getTitle(), $productType->getOrder(), $id));
		$this->db->trans_complete();
		
		if ($this->db->trans_status() === FALSE)
			return UPDATE_ERROR;
		
		// Update global variable: need to check performance
		Category_Model::getInstance()->getAllCategories();
		
		return ERROR_NONE;
	}
	
	public function updateOrder($ids, $orders)
	{
		$query = "UPDATE ".PRODUCT_TYPE_TABLE." SET _order = CASE id";
		$where = "(";
		
		for ($i = 0, $size = count($ids); $i < $size; $i++)
		{
			$query .= " WHEN ".$ids[$i]." THEN ".$orders[$i];
			$where .= $ids[$i];
			$where .= ($i < $size - 1) ? "," : "";
		}
		
		$where .= ")";
		$query .= " END WHERE id IN ".$where;
		
		$this->db->trans_start();
		$this->db->query($query, $ids);
		$this->db->trans_complete();
		
		if ($this->db->trans_status() === FALSE)
			return UPDATE_ERROR;
		
		return ERROR_NONE;
	}
	
	public function deleteMultipleProductTypes($ids)
	{
		$query = "";
		$size = count($ids);
		$query = "DELETE FROM ".PRODUCT_TYPE_TABLE." WHERE id IN (";
		
		for($i = 0; $i < ($size - 1); $i++)
		{
			$query .= "?, ";
		}
		
		$query .= "?)";
		
		$this->db->trans_start();
		$this->db->query($query, $ids);
		$this->db->trans_complete();
		
		if ($this->db->trans_status() === FALSE)
			return DELETE_ERROR;
		
		// Update global variable: need to check performance
		Category_Model::getInstance()->getAllCategories();
		
		return ERROR_NONE;
	}
	
	/*
	 Check if product type exists or not
	 */
	public function isProductTypeExisted_id($id)
	{
		$this->db->where('id', $id);
		$data = $this->db->get(PRODUCT_TYPE_TABLE);

		return ($data->num_rows() > 0);
	}

	public function findProductType($categoryId, $title)
	{
		$id = -1;
		
		$query = "SELECT id FROM " . PRODUCT_TYPE_TABLE . " WHERE category_id = ? and title LIKE N'%" . $this->db->escape_like_str($title) . "%'";
		$data = $this->db->query($query, array($categoryId));
		
		if ($data->num_rows() > 0)
		{
			$row = $data->row();
			$id = $row->id;
		}

		return $id;
	}
}
?>